using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using Microsoft.BizTalk.Message.Interop;

namespace Microsoft.Samples.BizTalk.Adapter.Tcp.Receive
{
    /// <summary>
    ///     This class represents a work item that contains data to be sent back to a client as a response.
    /// </summary>
    /// <author>
    ///     Paul Brown
    /// </author>
    public class TransmitWorkItem : IWorkItem
    {
        #region Private Instance Members

        private WorkItemStatusTypes _status;            // The status of the work item
        private ReceiveLocation     _receiveLocation;   // The receive location involved in the operation
        private IBaseMessage        _message;           // The message to transmit
        private Socket              _clientSocket;      // The client socket to send the message down

        #endregion

        #region Constructor

        public TransmitWorkItem(ReceiveLocation receiveLocation, IBaseMessage message, Socket clientSocket)
        {
            // Validate
            if (receiveLocation == null) throw new ArgumentNullException("receiveLocation");
            if (message == null) throw new ArgumentNullException("message");
            if (clientSocket == null) throw new ArgumentNullException("clientSocket");

            // Initialize
            this._status = WorkItemStatusTypes.Ready;
            this._receiveLocation = receiveLocation;
            this._message = message;
            this._clientSocket = clientSocket;
        }

        #endregion

        #region Public Instance Properties

        /// <summary>
        ///     This property returns the receive location involved in the operation.
        /// </summary>
        public ReceiveLocation ReceiveLocation
        {
            get { return (this._receiveLocation); }
        }

        /// <summary>
        ///     This property returns the message to be transmitted.
        /// </summary>
        public IBaseMessage Message
        {
            get { return (this._message); }
        }

        /// <summary>
        ///     This property returns the client socket that is waiting for the response.
        /// </summary>
        public Socket ClientSocket
        {
            get { return (this._clientSocket); }
        }

        #endregion

        #region IWorkItem Members

        #region Public Instance Properties

        /// <summary>
        ///     This property gets or sets the status of the work item.
        /// </summary>
        public WorkItemStatusTypes Status
        {
            get { return (this._status); }
            set { this._status = value; }
        }

        #endregion

        #region Public Instance Methods

        /// <summary>
        ///     This method saves the content of the work item to the folder location specified by the adapter.
        /// </summary>
        public void Save()
        {
            // Do nothing - this is for sending a response and we don't inform BizTalk that the response has been
            // sent, it will still be in the message box so no need to save.
        }

        #endregion

        #endregion
    }
}
